##########################################
## Copyright 2012-2013 Ceruti Francesco
##
##
## This file is part of LiSP (Linux Show Player).
##
## LiSP (Linux Show Player) is free software: you can redistribute it and/or modify
## it under the terms of the GNU Lesser General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## LiSP (Linux Show Player) is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU Lesser General Public License for more details.
##
## You should have received a copy of the GNU Lesser General Public License
## along with LiSP (Linux Show Player).  If not, see <http://www.gnu.org/licenses/>.
##########################################

from PyQt4 import QtCore
from PyQt4 import QtGui
from lisp.ui.preferences.preferences_widget import PreferencesWidget
import math

MIN_dB = 0.000000312  # -100dB


class AudioDynamic(PreferencesWidget):

    NAME = 'Compressor/Expander'

    def __init__(self, size, parent=None):
        super(AudioDynamic, self).__init__(size, parent)

        self.groupBox = QtGui.QGroupBox(self)
        self.groupBox.setGeometry(QtCore.QRect(0, 0, 431, 241))

        self.gridLayoutWidget = QtGui.QWidget(self.groupBox)
        self.gridLayoutWidget.setGeometry(QtCore.QRect(10, 30, 431, 191))
        self.gridLayout = QtGui.QGridLayout(self.gridLayoutWidget)
        self.gridLayout.setMargin(0)

        self.comboMode = QtGui.QComboBox(self.gridLayoutWidget)
        self.comboMode.addItem("Compressor")
        self.comboMode.addItem("Expander")
        self.gridLayout.addWidget(self.comboMode, 0, 0, 1, 1)

        self.comboCh = QtGui.QComboBox(self.gridLayoutWidget)
        self.comboCh.addItem("Soft Knee")
        self.comboCh.addItem("Hard Knee")
        self.gridLayout.addWidget(self.comboCh, 1, 0, 1, 1)

        self.spinRatio = QtGui.QDoubleSpinBox(self.gridLayoutWidget)
        self.gridLayout.addWidget(self.spinRatio, 2, 0, 1, 1)

        self.spinThreshold = QtGui.QDoubleSpinBox(self.gridLayoutWidget)
        self.spinThreshold.setMaximum(0)
        self.spinThreshold.setMinimum(-100)
        self.spinThreshold.setSingleStep(1)
        self.gridLayout.addWidget(self.spinThreshold, 3, 0, 1, 1)

        self.labelMode = QtGui.QLabel(self.gridLayoutWidget)
        self.labelMode.setAlignment(QtCore.Qt.AlignCenter)
        self.gridLayout.addWidget(self.labelMode, 0, 1, 1, 1)

        self.labelCh = QtGui.QLabel(self.gridLayoutWidget)
        self.labelCh.setAlignment(QtCore.Qt.AlignCenter)
        self.gridLayout.addWidget(self.labelCh, 1, 1, 1, 1)

        self.labelRatio = QtGui.QLabel(self.gridLayoutWidget)
        self.labelRatio.setAlignment(QtCore.Qt.AlignCenter)
        self.gridLayout.addWidget(self.labelRatio, 2, 1, 1, 1)

        self.labelThreshold = QtGui.QLabel(self.gridLayoutWidget)
        self.labelThreshold.setAlignment(QtCore.Qt.AlignCenter)
        self.gridLayout.addWidget(self.labelThreshold, 3, 1, 1, 1)

        self.retranslateUi()

    def retranslateUi(self):
        self.groupBox.setTitle("Audio Dynamic - Compressor/Expander")
        self.labelMode.setText("Type")
        self.labelCh.setText("Curve Shape")
        self.labelRatio.setText("Ratio")
        self.labelThreshold.setText("Threshold (dB)")

    def enableCheck(self, enable):
        self.groupBox.setCheckable(enable)
        self.groupBox.setChecked(False)

    def getConfiguration(self):
        conf = {}
        if(not (self.groupBox.isCheckable() and not self.groupBox.isChecked())):
            conf['audiodynamic'] = {'ratio': self.spinRatio.value(), 'threshold': math.pow(10, self.spinThreshold.value() / 20)}
            if(self.comboMode.currentIndex() == 0):
                conf['audiodynamic']['mode'] = 'compressor'
            else:
                conf['audiodynamic']['mode'] = 'expander'
            if(self.comboCh.currentIndex() == 0):
                conf['audiodynamic']['characteristics'] = 'soft-knee'
            else:
                conf['audiodynamic']['characteristics'] = 'hard-knee'

        return conf

    def setConfiguration(self, conf):
        if(conf is not None):
            if('audiodynamic' in conf):
                if(type(conf['audiodynamic']) == type({})):
                    if('mode' in conf['audiodynamic']):
                        if(conf['audiodynamic']['mode'] == 'compressor'):
                            self.comboMode.setCurrentIndex(0)
                        else:
                            self.comboMode.setCurrentIndex(1)
                    else:
                        self.comboMode.setEnabled(False)
                    if('ratio' in conf['audiodynamic']):
                        self.spinRatio.setValue(conf['audiodynamic']['ratio'])
                    else:
                        self.spinRatio.setEnabled(False)
                    if('characteristics' in conf['audiodynamic']):
                        if(conf['audiodynamic']['characteristics'] == 'soft-knee'):
                            self.comboCh.setCurrentIndex(0)
                        else:
                            self.comboCh.setCurrentIndex(1)
                    else:
                        self.comboCh.setEnabled(False)
                    if (conf['audiodynamic']['threshold'] == 0):
                        conf['audiodynamic']['threshold'] = MIN_dB
                    if('threshold' in conf['audiodynamic']):
                        self.spinThreshold.setValue(20 * math.log10(conf['audiodynamic']['threshold']))
                    else:
                        self.spinThreshold.setEnabled(False)
                else:
                    self.setEnabled(False)
            else:
                self.setEnabled(False)
